﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Texfina.Core.Common;  
using Texfina.Core.Web;
using Texfina.Common.Mg;
using Texfina.Core.Data;  
using Texfina.Entity.Rh;
using RHBOQry = Texfina.BOQry.Rh;
using RHBORpt = Texfina.BORpt.Rh;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using CrystalDecisions.Shared;

namespace RH
{
   public partial class wfGestionDescBnd : SearchPage 
   {
        public const int _GRDLIST_IDESTADO = 5;
    
      protected void Page_Load(object sender, EventArgs e)
      {
                       
         this.LoadInitialSettings();
         if(this.IsPostBack){
            this.LoadInPostBack();
            return;
         } 

         this.LoadData();
         SearchData();

      }

      public override void LoadInitialSettings()
      {
         Label lbl = (Label) this.Master.FindControl("lblTitle");
         lbl.Text = "Gestion de Descasos";
         this.Page.Form.DefaultButton = bttSearch.UniqueID;
      }

      public override void SearchData()
      {

          EPerVacaDescaBandeja objE = new EPerVacaDescaBandeja();

          objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();

          if (this.txtidVacaDesca.Text == "")
             objE.IdVacaDesca = null;
          else
             objE.IdVacaDesca = this.txtidVacaDesca.Text;
          
         if (this.txtidPersonal.Text == "")
            objE.IdPersonal = null;
         else
            objE.IdPersonal = this.txtidPersonal.Text;

         if (this.txtnuDocIden.Text == "")
            objE.NuDocIden  = null;
         else
            objE.NuDocIden = this.txtnuDocIden.Text;

         if (this.txtdsPersonal.Text == "")
            objE.DsApellNom = null;
         else
            objE.DsApellNom = this.txtdsPersonal.Text;

         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.PerVacaDesca));
         DataTable dt = objBO.GetByCriteria(objE);

         this.GridSetDataBinding(grdList, dt);

      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      { 
         DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
         FormEditAction formEA= FormEditAction.AddNew;
         this.EditRowIndex = Convert.ToInt32(e.CommandArgument);
         switch(e.CommandName)
         {
             case ConstGridRowCommand.GRIDEDIT:
               formEA = FormEditAction.Edit;
               break;
             case ConstGridRowCommand.GRIDDELETE:
               formEA = FormEditAction.Delete;
               break;
             case ConstGridRowCommand.GRIDPREVIEW:
               PrintReport(ConstGridRowCommand.GRIDPREVIEW);
               return;
         }
         String strQS = "wfGestionDescMnt.aspx?formEA=" + Convert.ToInt32(formEA).ToString() + "&IdVacaDesca=" + dr["idVacaDesca"].ToString() + "&IdPersonal=" + dr["idPersonal"].ToString();
         Response.Redirect(strQS);
      
      }

      protected void grdBttCmdPrevPostBackDataBinding(Object sender, EventArgs e)
      {
          ImageButton ctl = (ImageButton)sender;
          ScriptManager sm = ScriptManager.GetCurrent(this.Page);
          sm.RegisterPostBackControl(ctl);
      }

      public override void PrintReport(string key)
      {
          DataSet ds = (DataSet)this.GetDataReport(key);
          if (ds.Tables[0].Rows.Count > 0)
          {
              ReportDocument objRpt = new ReportDocument();
              //objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/RH_PerVacaDesca.rpt"));
              //objRpt.SetDataSource(ds.Tables[0]);
              //MemoryStream ms = new MemoryStream();
              //Response.Clear();
              //ms = (MemoryStream)objRpt.ExportToStream(ExportFormatType.PortableDocFormat);
              //objRpt.Dispose();
              //objRpt = null;

              //objRpt = new ReportDocument();
              objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/RH_PlaniVacaDesca.rpt"));
              objRpt.SetDataSource(ds.Tables[1]);
              MemoryStream msPlani = new MemoryStream();
              Response.Clear();
              msPlani = (MemoryStream)objRpt.ExportToStream(ExportFormatType.PortableDocFormat);
              objRpt.Dispose();
              objRpt = null;


              Response.AddHeader("Content-Disposition", "attachment;filename=Vacaciones.pdf");

              Response.Buffer = true;
              Response.ContentType = "application/pdf";

              msPlani.WriteTo(Response.OutputStream);
              Response.Flush();
              Response.End();
          }

      }

      public override object GetDataReport(string key)
      {
          EPerVacaDesca objE = new EPerVacaDesca();
          RHBORpt.IPerVacaDesca objBODet = (RHBORpt.IPerVacaDesca)WCFHelper.GetObject<RHBORpt.IPerVacaDesca>(typeof(RHBORpt.PerVacaDesca));

          DataRow dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);

          objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
          objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          objE.IdPersonal = dr["idPersonal"].ToString();
          objE.IdVacaDesca  = dr["idVacaDesca"].ToString();
          DataTable dt = objBODet.GetSolicitudVacaciones(objE);

          objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
          objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          objE.IdPersonal = dr["idPersonal"].ToString();
          objE.IdVacaDesca = dr["idVacaDesca"].ToString();
          DataTable dtDet = objBODet.GetPlanillaVacaciones(objE);

          DataSet ds = new DataSet();
          ds.Tables.Add(dt);
          ds.Tables.Add(dtDet);

          return ds;

      }


      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {


      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
          String strQS = "";

          switch (e.Item.Value)
          {
              case ConstMenuToolbarValue.BUTTON_NEW:
                  strQS = "wfGestionDescMnt.aspx?formEA=" + Convert.ToInt32(FormEditAction.AddNew).ToString() + "&IdVacaDesca=&IdPersonal=";
                  break;
          }
          Response.Redirect(strQS);
      }

      protected void bttOk_Click(object sender, EventArgs e)
      {
          this.SearchData();
      }

   }
}